/*
 * Created on Jan 10, 2009
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.general.genericUtils;

import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Gopi
 * 
 *         TODO To change the template for this generated type comment go to
 *         Window - Preferences - Java - Code Style - Code Templates
 */
public class DateUtils {
	String[] weekdays = new DateFormatSymbols().getWeekdays();
	String[] monthNames = new DateFormatSymbols().getMonths();
	Calendar today = Calendar.getInstance();
	Calendar customDay = Calendar.getInstance();
	int year = 0;
	int month = 0;

	String String1stDayOfMonth;
	Date weekBeginingDate;

	/**
	 * @return Returns the weekBeginingDate.
	 */
	public String getWeekBeginingDate(int weekNum, DateUtils du) {

		int totalDays = du.getCustomDay().getActualMaximum(
				Calendar.DAY_OF_MONTH);
		Calendar week = (Calendar) du.getCustomDay().clone();

		if (weekNum > 1) {
			int j = 9 - week.get(Calendar.DAY_OF_WEEK);
			week.set(Calendar.DAY_OF_MONTH, j + 1);
			int k = 2;
			for (int i = j; i <= totalDays; i += 7) {
				week.set(Calendar.DAY_OF_MONTH, i);
				if (k == weekNum) {
					break;
				}
				++k;
			}
		}
	    
//		SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		
		String s = format.format(week.getTime());
		return s;
	}

	/**
	 * @param weekBeginingDate
	 *            The weekBeginingDate to set.
	 */
	public void setWeekBeginingDate(Date weekBeginingDate) {
		this.weekBeginingDate = weekBeginingDate;
	}

	public Calendar getCustomDay() {
		return customDay;
	}

	public void setCustomDay(Calendar customDay) {
		this.customDay = customDay;
	}

	/**
	 * @return Returns the weekEndingDate.
	 */
	public String getWeekEndingDate(int weekNum, DateUtils du) {
		int totalDays = du.getCustomDay().getActualMaximum(
				Calendar.DAY_OF_MONTH);
		Calendar week = (Calendar) du.getCustomDay().clone();
		if (weekNum < 6) {
			int j = 8 - week.get(Calendar.DAY_OF_WEEK);
			week.set(Calendar.DAY_OF_MONTH, j + 1);
			int k = 1;
			for (int i = j; i <= totalDays; i += 7) {
				week.set(Calendar.DAY_OF_MONTH, i);
				if (k == weekNum) {
					break;
				}
				++k;
			}
		} else {
			week.set(Calendar.DAY_OF_MONTH, totalDays);
		}
//		SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String s = format.format(week.getTime());
		week = null;

		return s;
	}

	public static void main(String[] args) {
		DateUtils du = new DateUtils(2009, 4);
		du.getWeekBeginingDate(6, du);
		du.getWeekEndingDate(6, du);

	}

	/**
	 * @param weekEndingDate
	 *            The weekEndingDate to set.
	 */
	public void setWeekEndingDate(Date weekEndingDate) {
		this.weekEndingDate = weekEndingDate;
	}

	Date weekEndingDate;

	public DateUtils(int year, int month) {
		customDay.set(Calendar.YEAR, year);
		customDay.set(Calendar.MONTH, month);
		customDay.set(Calendar.DATE, 1);
		this.year = year;
		this.month = month;
	}

	/**
	 * @return the weekdays
	 */
	public String[] getWeekdays() {
		return weekdays;
	}

	/**
	 * @return the monthNames
	 */
	public String[] getMonthNames() {
		return monthNames;
	}

	/**
	 * @return the today
	 */
	public Calendar getToday() {
		return today;
	}

	/**
	 * @return the weekBeginingDate
	 */
	public Date getWeekBeginingDate() {
		return weekBeginingDate;
	}

	/**
	 * @return the weekEndingDate
	 */
	public Date getWeekEndingDate() {
		return weekEndingDate;
	}

	/**
	 * @param weekdays the weekdays to set
	 */
	public void setWeekdays(String[] weekdays) {
		this.weekdays = weekdays;
	}

	/**
	 * @param monthNames the monthNames to set
	 */
	public void setMonthNames(String[] monthNames) {
		this.monthNames = monthNames;
	}

	/**
	 * @param today the today to set
	 */
	public void setToday(Calendar today) {
		this.today = today;
	}

	public String getMonthName() {
		String monthName = monthNames[customDay.get(Calendar.MONTH)];
		monthName = monthName.substring(0, 3);
		return monthName;
	}

	public int getIntLastDayOfMonth() {
		return customDay.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	public String getStringLastDayOfMonth() {
		return weekdays[customDay.getActualMaximum(Calendar.DAY_OF_MONTH)];
	}

	public int getInt1stDayOfMonth() {
		return customDay.get(Calendar.DAY_OF_WEEK);
	}

	public String getString1stDayOfMonth() {
		return weekdays[customDay.get(Calendar.DAY_OF_WEEK)];
	}

	/**
	 * @return Returns the month.
	 */
	public int getMonth() {
		return month;
	}

	/**
	 * @param month
	 *            The month to set.
	 */
	public void setMonth(int month) {
		this.month = month;
	}

	/**
	 * @return Returns the year.
	 */
	public int getYear() {
		return year;
	}

	/**
	 * @param year
	 *            The year to set.
	 */
	public void setYear(int year) {
		this.year = year;
	}

	/**
	 * @param string1stDayOfMonth
	 *            The string1stDayOfMonth to set.
	 */
	public void setString1stDayOfMonth(String string1stDayOfMonth) {
		String1stDayOfMonth = string1stDayOfMonth;
	}

}
